home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C++
/
Applications
/
NeuroSim 1.0
/
.cp
/
CNeuroSimApp.cp
< prev
next >
Wrap
Text File
|
1996-02-19
|
9KB
|
325 lines
// ===========================================================================
// CNeuroSimApp.cp ©1996 Timo Eloranta
// ===========================================================================
// An application class derived from the PowerPlant LApplication class.
// This class handles the menu commands and owns the neural net and the
// window inside of which our pane object is.
#include "CNeuroSimApp.h"
#include <LApplication.h> //
#include <LEditField.h> //
#include <LCaption.h> //
#include <LDialogBox.h> //
#include <LGroupBox.h> // PowerPlant classes
#include <LGrowZone.h> //
#include <LPicture.h> //
#include <LStdControl.h> //
#include <LTabGroup.h> //
#include <LWindow.h> //
#include <UDesktop.h> // Deactivate & Activate
#include <UDrawingState.h> // UQDGlobals
#include <UModalDialogs.h> // StDialogHandler
#include <UMemoryMgr.h> // InitializeHeap
#include <URegistrar.h> // RegisterClass
#include <PP_Messages.h> // cmd_New, msg_OK
#include "CStdNeuralNet.h" // The only net type we have implemented
#include "CNeuroSimPane.h" // Our pane class
#include "CParamsDialog.h" // Our dialog class
#include "CMyCaption.h" // My own caption class
#include "Slider.h" // Scott Squires' slider class
// ===========================================================================
// • Main Program
// ===========================================================================
// A typical main function of a PowerPlant based Mac application
void main()
{
// Set Debugging options
#ifdef Debug_Throw
SetDebugThrow_(debugAction_Alert);
#endif
#ifdef Debug_Signal
SetDebugSignal_(debugAction_Alert);
#endif
InitializeHeap(3); // Initialize Memory Manager
// Initialize standard Toolbox managers
UQDGlobals::InitializeToolbox( &qd );
// Initialize randomizer seed
::GetDateTime((unsigned long *)&qd.randSeed);
new LGrowZone(20000); // Install a GrowZone function to catch
// low memory situations.
CNeuroSimApp theApp; // Create instance of our application
theApp.Run(); // class and run it
}
// ===========================================================================
// • CNeuroSimApp Class
// ===========================================================================
// ---------------------------------------------------------------------------
// • CNeuroSimApp
//
// Called by: main()
// ---------------------------------------------------------------------------
// Constructor
CNeuroSimApp::CNeuroSimApp()
{
RegisterClasses();
// Initialize parameters with default values
// (the defaults are defined in NS_Types.h).
SGenParams theTemp
= { DEFAULT_LENGTH_X_AVG, DEFAULT_LENGTH_Y_AVG,
DEFAULT_LENGTH_X_DEV, DEFAULT_LENGTH_Y_DEV,
DEFAULT_NET_SIZE,
DEFAULT_QTY_MIN, DEFAULT_QTY_MAX };
mParams = theTemp;
mNet = NULL;
mWindow = NULL;
}
// ---------------------------------------------------------------------------
// • ~CNeuroSimApp
// ---------------------------------------------------------------------------
// Destructor
CNeuroSimApp::~CNeuroSimApp()
{
if ( mWindow ) delete mWindow;
if ( mNet ) delete mNet;
}
// ---------------------------------------------------------------------------
// • ObeyCommand
//
// Called by: LCommander::ProcessCommand
// ---------------------------------------------------------------------------
// Respond to commands
Boolean
CNeuroSimApp::ObeyCommand(
CommandT inCommand,
void *ioParam)
{
Boolean cmdHandled = true;
CParamsDialog * theDialog;
switch (inCommand) {
// cmd_New, cmd_Params and cmd_Demo are menu commands
case cmd_New:
InitNewNet();
break;
case cmd_Params:
theDialog = (CParamsDialog *)
LWindow::CreateWindow( WIND_Params, this);
theDialog -> InitDialog();
theDialog -> SetValues( mParams );
theDialog -> Show();
break;
case cmd_Demo:
if ( mNet )
mNet -> ChangeDemoMode();
break;
// cmd_SetParams is the command which is sent by
// the parameters dialog if the user presses "OK".
// We respond to it by reading in the new parameter
// values before throwing the dialog away.
case cmd_SetParams:
theDialog = (CParamsDialog *)
((SDialogResponse *) ioParam) -> dialogBox;
theDialog -> GetValues( mParams );
delete theDialog;
break;
// By default we pass the command to our base class
// which handles cmd_About and cmd_Quit
default:
cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
break;
}
return cmdHandled;
}
// ---------------------------------------------------------------------------
// • FindCommandStatus
//
// Called by: LCommander::ProcessCommandStatus
// ---------------------------------------------------------------------------
// Pass back whether a Command is enabled and/or marked (in a Menu)
void
CNeuroSimApp::FindCommandStatus(
CommandT inCommand,
Boolean &outEnabled,
Boolean &outUsesMark,
Char16 &outMark,
Str255 outName)
{
outUsesMark = false;
switch (inCommand) {
case cmd_New:
case cmd_Params:
outEnabled = true;
break;
// The text of the "demo mode" menu item is set dynamically
// to "Start Demo" or "Stop Demo" depending on whether the
// mode is currently ON or OFF.
case cmd_Demo:
if (mNet) {
outEnabled = true;
if (mNet -> DemoModeOn())
::GetIndString( outName, STRx_Menus, str_StopDemo );
else
::GetIndString( outName, STRx_Menus, str_StartDemo );
} else
outEnabled = false;
break;
// We let our base class handle all the other commands
default:
LApplication::FindCommandStatus(inCommand, outEnabled,
outUsesMark, outMark, outName);
break;
}
}
// ---------------------------------------------------------------------------
// • InitNewNet
//
// Called by: CNeuroSimApp::ObeyCommand
// ---------------------------------------------------------------------------
// Create a new neural net and the window to display it in.
void
CNeuroSimApp::InitNewNet()
{
CNeuroSimPane * thePane;
// Throw out the old stuff if any
if ( mWindow ) delete mWindow;
if ( mNet ) delete mNet;
// Create and initialize a new net object.
mNet = new CStdNeuralNet( mParams );
// Create the main window from a resource
// (our pane is inside the window)
mWindow = LWindow::CreateWindow( WIND_NeuroSim, this );
thePane = (CNeuroSimPane *) mWindow -> FindPaneByID( pane_NeuralNet );
// "Introduce" the net and the pane to each other.
thePane -> SetNet( mNet );
mNet -> SetPane( thePane );
mWindow -> Show();
}
// ---------------------------------------------------------------------------
// • ShowAboutBox
//
// Called by: LApplication::ObeyCommand
// ---------------------------------------------------------------------------
// Display the About Box for NeuroSim
void
CNeuroSimApp::ShowAboutBox()
{
StDialogHandler theHandler( WIND_NeuroSimAbout, this );
LWindow *theDialog = theHandler.GetDialog();
if ( theDialog ) {
while ( true ) {
MessageT hitMessage = theHandler.DoDialog();
if ( hitMessage == msg_OK )
break;
}
} else { // This is only for the case where
UDesktop::Deactivate(); // we don't have enough memory
::Alert( ALRT_About, nil ); // to show the finer About Box
UDesktop::Activate(); // requested above...
}
}
// ---------------------------------------------------------------------------
// • RegisterClasses
//
// Called by: CNeuroSimApp::CNeuroSimApp
// ---------------------------------------------------------------------------
// Register classes for objects created from 'PPob' resources
void
CNeuroSimApp::RegisterClasses()
{
// --- First the PowerPlant classes --- //
URegistrar::RegisterClass( LCaption ::class_ID,
LCaption ::CreateCaptionStream);
URegistrar::RegisterClass( LDialogBox ::class_ID,
LDialogBox ::CreateDialogBoxStream);
URegistrar::RegisterClass( LEditField ::class_ID,
LEditField ::CreateEditFieldStream);
URegistrar::RegisterClass( LGroupBox ::class_ID,
LGroupBox ::CreateGroupBoxStream);
URegistrar::RegisterClass( LPane ::class_ID,
LPane ::CreatePaneStream);
URegistrar::RegisterClass( LPicture ::class_ID,
LPicture ::CreatePictureStream);
URegistrar::RegisterClass( LStdButton ::class_ID,
LStdButton ::CreateStdButtonStream);
URegistrar::RegisterClass( LTabGroup ::class_ID,
LTabGroup ::CreateTabGroupStream);
URegistrar::RegisterClass( LView ::class_ID,
LView ::CreateViewStream);
URegistrar::RegisterClass( LWindow ::class_ID,
LWindow ::CreateWindowStream);
// --- Then the non-PowerPlant classes --- //
URegistrar::RegisterClass( HorzSlider ::class_ID,
HorzSlider ::CreateHorzSliderStream);
URegistrar::RegisterClass( CNeuroSimPane ::class_ID,
CNeuroSimPane ::CreateNeuroSimPaneStream);
URegistrar::RegisterClass( CParamsDialog ::class_ID,
CParamsDialog ::CreateParamsDialogStream);
URegistrar::RegisterClass( CMyCaption ::class_ID,
CMyCaption ::CreateMyCaptionStream);
}